package com.ctshk.rpc.system.service.impl;

import java.util.List;

import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.system.dto.QuerySupplierCostItemDTO;
import com.ctshk.rpc.system.entity.SysSupplier;
import com.ctshk.rpc.system.entity.SysSupplierCostItem;
import com.ctshk.rpc.system.mapper.SysSupplierCostItemMapper;
import com.ctshk.rpc.system.mapper.SysSupplierMapper;
import com.ctshk.rpc.system.req.QuerySupplierCostItemReq;
import com.ctshk.rpc.system.service.ISysSupplierCostItemService;

/**
 * <p>
 * 供应商成本项 服务实现类
 * </p>
 *
 * @author 张志朋
 * @since 2021-02-05
 */
@DubboService
public class SysSupplierCostItemServiceImpl extends ServiceImpl<SysSupplierCostItemMapper, SysSupplierCostItem> implements ISysSupplierCostItemService {
	@Autowired
    private SysSupplierCostItemMapper sysSupplierCostItemMapper;
	
	@Autowired
    private SysSupplierMapper sysSupplierMapper;
	
	/**查询供应商成本项
	 * @param req
	 * @return
	 */
    @Override
    public Result<List<QuerySupplierCostItemDTO>> querySupplierCostItem(QuerySupplierCostItemReq req) {
		QueryWrapper<SysSupplierCostItem> queryWrapper=new QueryWrapper<>();
    	queryWrapper.lambda().eq(SysSupplierCostItem::getSupplierId, req.getSupplierId())
				.eq(SysSupplierCostItem::getIsDeleted, IsDeletedCode.NO.getCode());
    	
    	List<SysSupplierCostItem> costItems=sysSupplierCostItemMapper.selectList(queryWrapper);
    	List<QuerySupplierCostItemDTO> dtoList = EntityUtil.copyList(costItems, QuerySupplierCostItemDTO.class);
    	for (QuerySupplierCostItemDTO item : dtoList) {
			QueryWrapper<SysSupplier> supplierWrapper=new QueryWrapper<>();
			supplierWrapper.lambda().eq(SysSupplier::getId, item.getSupplierId())
				.eq(SysSupplier::getIsDeleted,IsDeletedCode.NO.getCode());
			SysSupplier sysSupplier = sysSupplierMapper.selectOne(supplierWrapper);
			if(sysSupplier != null){
				item.setCnName(sysSupplier.getCnName());
				item.setPlatformSysType(sysSupplier.getPlatformSysType());
			}
		}
    	return Result.success(dtoList);
	}
}
